package org.tgcloud.group.service.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tgcloud.group.core.APPResultMessage;
import org.tgcloud.group.core.ResultMessage;
import org.tgcloud.group.core.StringUtil;
import org.tgcloud.group.repository.MjBusinessPersonMapper;
import org.tgcloud.group.repository.SysLabelValueMapper;
import org.tgcloud.group.service.manager.OrderManagerService;
import org.tgcloud.group.web.controller.dto.order.OrderRateDTO;
import org.tgcloud.group.web.controller.dto.sys.SysLabelValueListDTO;
import org.tgcloud.group.web.controller.req.sysLabel.SysLabelIdReq;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: zhy
 * @create: 2023-09-21 17:20
 * @description:
 */
@Service
public class SysService {

    @Autowired
    private SysLabelValueMapper sysLabelValueMapper;

    @Autowired
    private MjBusinessPersonMapper businessPersonMapper;

    @Autowired
    private OrderManagerService orderManagerService;


    public APPResultMessage getSysLabelValueManageList(SysLabelIdReq req) {
        Map<Integer, List<SysLabelValueListDTO>> map = new HashMap();
        String[] split = null;
        if (StringUtil.isNotBlank(req.getSysLabelIds())){
            split = req.getSysLabelIds().split(",");
        }
        if (split == null || split.length == 0){
            return APPResultMessage.getSuccessMessage();
        }
        for (String id : split){
            Integer labelId = Integer.valueOf(id);
            if (labelId == 7){
                List<SysLabelValueListDTO> listDTOS = businessPersonMapper.selectListBySysLabelId(req.getKeyword());
                map.put(Integer.valueOf(id), listDTOS);
            }else {
                List<SysLabelValueListDTO> listDTOS = sysLabelValueMapper.selectListBySysLabelId(labelId, null,0);
                if (labelId == 44){
                    for (SysLabelValueListDTO dto : listDTOS){
                        OrderRateDTO rateDTO = orderManagerService.doGetRateContent(dto.getId());
                        dto.setSysValueName(dto.getSysValueName() + " - " + rateDTO.getAppContent());
                    }
                }
                map.put(Integer.valueOf(id), listDTOS);
            }
        }
        ResultMessage resultMessage = ResultMessage.builder().setData(map);
        APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
        successMessage.setResult(resultMessage);
        return successMessage;
    }
}
